package com.ruijie.acl.web.validator;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.apache.commons.lang.StringUtils;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Validator;

import com.ruijie.foundation.international.MessageSupport;

/**
 * <p>
 * Title: 时间验证
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 	时间合法性验证
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-07-14
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("timeValueValidator")
@Validator
public class TimeValueValidator extends MessageSupport implements
		javax.faces.validator.Validator {

	@In private Map<String, String> messages;
	
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		// 验证 数值:数值
		String text = value.toString().trim();

		if(component.getAttributes().get("required").toString().equals("true")){
			if(!StringUtils.isNotEmpty(text.replaceAll("　",   " ").trim())){
				throw new ValidatorException(new FacesMessage(messages.get("javax.faces.component.UIInput.REQUIRED")));
			}
		}
		
		String regex="([0-9]|([0-1][0-9])|(2[0-3])):([0-9]|([0-5][0-9]))";
		if(!text.matches(regex)){
			throw new ValidatorException(new FacesMessage(
					getI18NMessage("acl_validation_time")));
		}
	}

}
